﻿using QS.Tool.Common.EncryptDecrypt;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QS.Tool.MainEntry
{
    public partial class FrmEncryptDecrypt : Form
    {
        public FrmEncryptDecrypt()
        {
            InitializeComponent();
            cmbSKey.SelectedIndex = cmbSKey.Items.IndexOf("8位");
        }

        #region MD5
        private void btnEncryptMD5_Click(object sender, EventArgs e)
        {
            try
            {
                var inputMsg = txtInput.Text;
                if (string.IsNullOrWhiteSpace(inputMsg))
                {
                    MessageBox.Show("输入值不能为空", "提示");
                }
                else
                {
                    string resultMsg = "";
                    if (rdb16.Checked)
                    {
                        resultMsg = MD5Helper.MD5Encrypt16(inputMsg);
                    }
                    else if (rdb64.Checked)
                    {
                        resultMsg = MD5Helper.MD5Encrypt64(inputMsg);
                    }
                    else if (rdb32Up.Checked)
                    {
                        resultMsg = MD5Helper.MD5Encrypt32ToUpper(inputMsg);
                    }
                    else
                    {
                        resultMsg = MD5Helper.MD5Encrypt32ToLower(inputMsg);
                    }
                    txtResult.Text = resultMsg;
                }
            }
            catch (Exception ex)
            {
                txtResult.Text = ex.Message;
            }
        }

        private void btnCopyMD5_Click(object sender, EventArgs e)
        {
            var copyMsg = txtResult.Text;
            if (!string.IsNullOrWhiteSpace(copyMsg))
            {
                Clipboard.SetDataObject(copyMsg);
                MessageBox.Show("复制成功，现在可以直接粘贴使用", "提示");
            }
            else
            {
                MessageBox.Show("内容为空，无法复制", "提示");
            }
        }
        #endregion

        #region Unicode
        private void btnUnicode_Click(object sender, EventArgs e)
        {
            try
            {
                var inputMsg = txtInputUni.Text;
                if (string.IsNullOrWhiteSpace(inputMsg))
                {
                    MessageBox.Show("输入值不能为空", "提示");
                }
                else
                {
                    string resultMsg = "";
                    if (rdbStrToUnicode.Checked)
                    {
                        resultMsg = UnicodeHelper.StringToUnicode(inputMsg);
                    }
                    else if (rdbUnicodeToStr.Checked)
                    {
                        resultMsg = UnicodeHelper.UnicodeToString(inputMsg);
                    }
                    txtResultUni.Text = resultMsg;
                }
            }
            catch (Exception ex)
            {
                txtResultUni.Text = ex.Message;
            }
        }

        private void btnCopyUnicode_Click(object sender, EventArgs e)
        {
            var copyMsg = txtResultUni.Text;
            if (!string.IsNullOrWhiteSpace(copyMsg))
            {
                Clipboard.SetDataObject(copyMsg);
                MessageBox.Show("复制成功，现在可以直接粘贴使用", "提示");
            }
            else
            {
                MessageBox.Show("内容为空，无法复制", "提示");
            }
        }
        #endregion

        #region DES
        private void btnDESEncrypt_Click(object sender, EventArgs e)
        {
            try
            {
                #region MyRegion
                string sKey = txtDESSecretKey.Text;
                var inputMsg = txtInputDES.Text;
                if (string.IsNullOrWhiteSpace(sKey))
                {
                    MessageBox.Show("密钥值不能为空", "提示");
                    return;
                }
                if (string.IsNullOrWhiteSpace(inputMsg))
                {
                    MessageBox.Show("输入值不能为空", "提示");
                }
                else
                {
                    var cmb = cmbSKey.Text;
                    string resultMsg = "";
                    switch (cmb)
                    {
                        case "8位":
                            if (rdbDES16.Checked)
                            {
                                resultMsg = DESHelper.EncryptASCII(inputMsg, sKey);
                            }
                            else
                            {
                                resultMsg = DESHelper.EncryptStringASCII(inputMsg, sKey);
                            }
                            break;
                        case "4位":
                            if (rdbDES16.Checked)
                            {
                                resultMsg = DESHelper.EncryptUnicode(inputMsg, sKey);
                            }
                            else
                            {
                                resultMsg = DESHelper.EncryptStringUnicode(inputMsg, sKey);
                            }
                            break;
                    }
                    txtResultDES.Text = resultMsg;
                }
                #endregion
            }
            catch (Exception ex)
            {
                txtResultDES.Text = ex.Message;
            }
        }

        private void btnDESDecrypt_Click(object sender, EventArgs e)
        {
            try
            {
                #region MyRegion
                string sKey = txtDESSecretKey.Text;
                var inputMsg = txtInputDES.Text;
                if (string.IsNullOrWhiteSpace(sKey))
                {
                    MessageBox.Show("密钥值不能为空", "提示");
                    return;
                }
                if (string.IsNullOrWhiteSpace(inputMsg))
                {
                    MessageBox.Show("输入值不能为空", "提示");
                }
                else
                {
                    var cmb = cmbSKey.Text;
                    string resultMsg = "";
                    switch (cmb)
                    {
                        case "8位":
                            if (rdbDES16.Checked)
                            {
                                resultMsg = DESHelper.DecryptASCII(inputMsg, sKey);
                            }
                            else
                            {
                                resultMsg = DESHelper.DecryptStringASCII(inputMsg, sKey);
                            }
                            break;
                        case "4位":
                            if (rdbDES16.Checked)
                            {
                                resultMsg = DESHelper.DecryptUnicode(inputMsg, sKey);
                            }
                            else
                            {
                                resultMsg = DESHelper.DecryptStringUnicode(inputMsg, sKey);
                            }
                            break;
                    }
                    txtResultDES.Text = resultMsg;
                }
                #endregion
            }
            catch (Exception ex)
            {
                txtResultDES.Text = ex.Message;
            }
        }

        private void btnDESCopy_Click(object sender, EventArgs e)
        {
            var copyMsg = txtResultDES.Text;
            if (!string.IsNullOrWhiteSpace(copyMsg))
            {
                Clipboard.SetDataObject(copyMsg);
                MessageBox.Show("复制成功，现在可以直接粘贴使用", "提示");
            }
            else
            {
                MessageBox.Show("内容为空，无法复制", "提示");
            }
        } 
        #endregion
    }
}
